Skip to content

fix: preserve thinking/redacted_thinking blocks in Anthropic message transforms#23755

Open
bbartels wants to merge 15 commits intoanomalyco:devfrom
bbartels:dev
Open

fix: preserve thinking/redacted_thinking blocks in Anthropic message transforms#23755
bbartels wants to merge 15 commits intoanomalyco:devfrom
bbartels:dev

Conversation

@bbartels
Copy link
Copy Markdown
Contributor

Issue for this PR

Closes #14332

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Fixes the Anthropic API error: messages.X.content.Y: 'thinking' or 'redacted_thinking' blocks in the latest assistant message cannot be modified.
The Anthropic API requires that thinking and redacted_thinking blocks in assistant messages are sent back byte-for-byte identical in subsequent requests. Three code paths in normalizeMessages / applyCaching in src/provider/transform.ts were inadvertently modifying these blocks:

  1. Empty content filtering removed redacted_thinking blocks. The AI SDK represents these as { type: "reasoning", text: "", providerOptions: { anthropic: { redactedData: "..." } } } — the empty text caused them to be filtered out. Fix: only filter reasoning parts that have no providerOptions.
  2. Tool-use reordering split assistant messages containing [thinking, tool_call, text] into two separate messages, changing which message the thinking blocks belong to. Fix: skip the reorder when reasoning blocks are present.
  3. Cache control hints could be applied to a reasoning block if it was the last content block in the message. Fix: find the last non-reasoning block for cache annotations instead.

How did you verify your code works?

Added tests covering all three fixes to test/provider/transform.test.ts. All 143 tests pass (130 existing + 13 new).
bun test test/provider/transform.test.ts
143 pass, 0 fail

Screenshots / recordings

N/A - not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions
Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Potential Duplicate/Related PRs Found:

  1. PR fix(provider): skip empty-text filtering for assistant messages in normalizeMessages (#16748) #16750 - fix(provider): skip empty-text filtering for assistant messages in normalizeMessages

    • Directly related to the empty content filtering issue addressed in the current PR
  2. PR fix(provider): preserve assistant message content when reasoning blocks present #21370 - fix(provider): preserve assistant message content when reasoning blocks present

    • Addresses similar issue of preserving assistant messages when reasoning blocks are present
  3. PR fix(provider): preserve redacted_thinking blocks and fix signature validation #12131 - fix(provider): preserve redacted_thinking blocks and fix signature validation

    • Related to preserving redacted_thinking block integrity
  4. PR fix: replace empty text in reasoning messages to preserve thinking block positions #21860 - fix: replace empty text in reasoning messages to preserve thinking block positions

    • Similar fix for preserving thinking block positions with empty text handling

These PRs appear to be addressing overlapping issues with preserving thinking/reasoning blocks in Anthropic message transforms. The current PR (23755) consolidates fixes for three specific code paths (empty content filtering, tool-use reordering, and cache control hints) that were inadvertently modifying these blocks.

@bbartels
Copy link
Copy Markdown
Contributor Author

@rekram1-node Any chance you could have a look? ~20 LOC of actual changes, rest is tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Amazon Bedrock Opus 4.6 compaction failure

4 participants